#!/usr/bin/perl

# ls -lRe list date checker
# Simon Pollard, Apr 98
# No rights even remotely reserved, do whatever you like

if($#ARGV != 1) {
  print "Format: update.pl <directory to scan> <html output filename>\n";
  exit 1;
}

if(! -d $ARGV[0]) {
  print "That directory ($ARGV[0]) doesn't exist.\n";
  exit 1;
}
$sourceDir = $ARGV[0]; # the directory to 'ls'
$outputDir = (getpwnam('ftpadmin'))[7] . "/scripts/output"; # the output here
$scriptDir = (getpwnam('ftpadmin'))[7] . "/scripts"; # script + template

$template = "$scriptDir/updateTmpl";
$html = "$outputDir/$ARGV[1].html";
$exclude = "$outputDir/excludes";

$fileroot = "http://freenet.barnet.ac.uk/$ARGV[1]/";

%months = ("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04",
           "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08",
           "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12");

chdir($sourceDir);

# assumptions - permissions are 10 chars long, dates are 24 chars long
# *** files cannot have # character in them ***

# open a pipe from the ls command
open(SRC,"/bin/ls -lRe |") || die "Error opening ls output: $!";

open(TEMPL,"<$template") || die "Error opening template file: $!";
open(OUT,">$html") || die "Error opening HTML output file: $!";

open(EXCLUDES,"<$exclude") || die "Error opening excludes file: $!";
while(<EXCLUDES>) { chop; push(@excludes,$_); }
close(EXCLUDES);

while(<TEMPL>) {
  last if /^OUTPUT HERE$/;
  print OUT ;
}

# get current date

($sec,$min,$hr,$mday,$mn,$yr) = localtime();
$lyr = $yr + 1900; $lmn = $mn; $ldy = $mday;
## $lmn--;  - you don't have to do this, because months are numbered
##            from 0 when you use localtime()             
if($lmn == 0) { $lmn = 12; $lyr--; }

if($lmn < 10) { $lmn = "0$lmn"; }
if($ldy < 10) { $ldy = "0$ldy"; }
if($hr < 10) { $hr = "0$hr"; }
if($min < 10) { $min = "0$min"; }
if($sec < 10) { $sec = "0$sec"; }
# trust me, it's all necessary
# the time/date 'keys' must all be the same length
$lkey = "$lyr$lmn$ldy$hr$min$sec";

# stuff at the top of the page

$what = $ARGV[1];
$what =~ s/^(.)(.+)$/\u$1$2/;

print OUT "<h3><u>$what uploads for the past month (since $mday/$lmn/$lyr)</u></h3>\n";

print OUT <<TBLHD;
<table>
<tr>
 <th align="left">Author</th><th align="right">Size</th><th colspan=2>Date Updated</th><th align="left">File</th>
</tr>
TBLHD

if(eof(SRC)) { print "No files.  Exiting.\n"; exit 1; }

# read in ls -lRe

$auth = "<none>"; $rest = "";

while(<SRC>) {
  if(/^$/) { # (a blank line)
    $auth = "<none>";
    $rest = "";
    next;
  }
  if(($thisauth) = /^(\S+):$/) { # if the line looks like '<something>:'
    $auth = $thisauth;           # the author is <something>
    if($thisauth =~ tr/\//\//) { # if there's some /s in it
      ($auth) = ($thisauth =~ /^([^\/]+)\//); # the author's the stuff b4 them
      ($rest) = ($thisauth =~ /^$auth\/(.+)$/); # and the rest is what's after
      $rest .= "/";
    }
    next;
  }
  ($perm,$ln,$usr,$grp,$size,$date,$name) = # pretty eh? don't you love perl :)
    /^(.{10})\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(.{24})\s+(.+)$/;
  if(($perm =~ /^-/) && ($auth ne "<none>") && (! grep(($name eq $_),@excludes))) {
                     # it's a file with an author which isn't excluded
    push(@out,"$auth#$rest$name#$size#$date"); # add line to the array
  }
}

close(SRC);

# ok, we have a nice array of files.  now to sort 'em by date.

foreach (@out) {
  # first, make a nice date numbery thing to sort them by
  ($auth,$file,$size,$date) = split('#',$_);
  ($wkdy,$mo,$dy,$hr,$mn,$sc,$yr) = ($date =~ /^(\w+) (\w+)\s+(\d+) (\d+):(\d+):(\d+) (\d+)$/);
  if($dy < 10) { $dy="0$dy"; }
  $mono = $months{$mo}; # find the month number from the month name
  if(! $mono) {
    print "Corrupt file or a new month has been invented.\n$_"; # you'd hope not ;)
    exit 1;
  }
  $keydate = "$yr$mono$dy$hr$mn$sc"; # pretty key. looks like 19980418153025
  if($keydate > $lkey) {             # (Apr 18th 1998, 15:30.25)
    $final{$keydate} = "$auth#$file#$size#$mo $dy#$hr:$mn";
  }
}

@sortout = sort {$b <=> $a} keys %final;

# finally, the output!
# prettifying and html'ing

foreach $key (@sortout) {
  ($auth,$file,$size,$date,$time) = split('#',$final{$key});
  print OUT "<tr><td>$auth</td>";
  $type = "b";
  if($size > 1024) { $size /= 1024; $type = "k"; }
  if($size > 1024) { $size /= 1024; $type = "M"; }
  if($type eq "b") { $sz = $size; } else { $sz = sprintf("%.2f",$size); }
  print OUT "<td align=\"right\">$sz$type</td>";
  print OUT "<td>$date</td><td>$time</td>";
  print OUT "<td><a href=\"$fileroot$auth/$file\">$file</a></td></tr>\n";
}

print OUT "</table>\n";

print OUT "<hr><p>This file was auto generated on ",`date`,"</p>\n";

while(<TEMPL>) { print OUT ; }

close(TEMPL); close(OUT);

# the end :)
